package com.ambow.dao;

import java.util.List;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.ambow.Entity.Supplier;
import com.ambow.Entity.TextBook;
import com.ambow.utils.HibernateUtil;

public class Supplier1Dao {
	public boolean addSupplier(String new_name, String new_man, String new_tel,String new_address,
			String new_mail,String new_remark ){
       
	    Configuration conf = new Configuration().configure();
		SessionFactory sf=conf.buildSessionFactory();
		Session session = sf.openSession();
		Transaction tx = session.beginTransaction();
		
		Supplier sp = new Supplier();
		
		sp.setName(new_name);
		sp.setContactMan(new_man);
		sp.setTel(new_tel);
		sp.setAddress(new_address);
		sp.setEmail(new_mail);
		sp.setRemark(new_remark);
		
		session.save(sp);
		session.persist(sp);
		
		tx.commit();
		session.close();
		sf.close();
		
		return true;
		    
	}

	



	public boolean updateinfo(int id, String name, String contactman, String tel, String address, String email, String remark){
		boolean f=false;
		try {

	        HibernateUtil hibernateUtil = new HibernateUtil();
		    Session session = hibernateUtil.getSession();
		    session.beginTransaction();
		    Query query = session.createQuery("update Supplier s set s.name='"+name +"', s.contactMan='"+contactman+"',s.tel='"+tel+"',s.address='"+address+"',s.email='"+email+"',s.remark='"+remark+"' where s.id="+id);
		    query.executeUpdate();
		    session.getTransaction().commit();
		    
		    f=true;
		 }
	    catch (Exception e) {
	        // TODO Auto-generated catch block
	    	System.out.println("sdada");
	        e.printStackTrace();
        }
		return f;
	}

	public List allsupplierQuery(){
		List list=null;
//		String role="学生管理员";
		HibernateUtil hibernateUtil = new HibernateUtil();
	    Session session = hibernateUtil.getSession();
		
		Query query = session.createQuery("from Supplier");
		list=query.list();
		
	
		
		return list;
	}
	public List supplierQuery(String name){
		List<TextBook> list=null;
//		String role="学生管理员";
		HibernateUtil hibernateUtil = new HibernateUtil();
	    Session session = hibernateUtil.getSession();
		
		//from TextBook where name like ? and author like ? and publish like ?
		//from TextBook t where t.book_name='"+name+"' and t.author='"+author+"' and t.publish='"+publish+"' 
		Query query = session.createQuery("from Supplier where name like ? ");
		   query.setString(0,name);
		list=query.list();
		
		
		return list;
	}
	public boolean deleteSupplier(int id){

		HibernateUtil hibernateUtil = new HibernateUtil();
		Session session = hibernateUtil.sessionFactory.openSession();

		session.beginTransaction();
		
		Query query = session.createSQLQuery("delete from Supplier where id="+id+"");
		query.executeUpdate();
		session.getTransaction().commit();
		session.close();
	    return true;
	
		
	}
}
